package com.hmdp.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Result queryTypeList() {
        // 查询缓存中是否存在shopType
        String shopType = stringRedisTemplate.opsForValue().get("Shop:Type");
        if (shopType!= null) {
            List<Object> list = JSONUtil.parseArray(shopType);
            return Result.ok(list);
        }
        List<ShopType> typeList = this
                .query().orderByAsc("sort").list();
        String JsonType = JSONUtil.toJsonStr(typeList);
        stringRedisTemplate.opsForValue().set("Shop:Type",JsonType);
        return Result.ok(typeList);
    }
}
